<?php
/*
 MRSS2.INC.PHP by martin $ 2009/12/12 01:22:25
*/

$db = DB.'images_linked.db';
$root = ROOT_PATH;
$blog_name = BLOG_NAME;
$mod_date = date('D, d M Y H:i:s +0900', time());
$author = ONAME;

if(is_file($db)){
 $images = file($db);
} else return;

$_images = array();

foreach ($images as $img){
 if(substr($img, 10, 1)=='d' || strpos($img, '.png') || strpos($img, '.gif') || strpos($img, '.bmp')) continue;
 $_images[] = $img;
}
$images = $_images;

$images = array_slice($images, 0, 100);
shuffle($images);
shuffle($images); // again

$ITEMS = '';

foreach ($images as $info){
 $split = explode("|", $info); 
 $len = sizeof($split);// IMG: 9, 12 <
 $id = preg_replace('{^0+}', '', $split[0]); $name = $split[2]; $title = $split[7];
 $w = $split[3]; $h = $split[4];
 if(strpos($title, ',')){
  list($title, $_title) = explode(',', $title);
  $link = MODE_STATIC ? $root.date('Ym', $id).'-'.$_title.EXT_STATIC : $root.'index.php?UID='.$id;
 } else {
  $link = MODE_STATIC ? $root.$id.EXT_STATIC : $root.'index.php?UID='.$id;
 }
 $ITEMS .= NL.'  <item>
   <title>'.$title.' - '.date('Y/m/d', $id).'</title>
   <link>'.$link.'</link>
   <author>'.$author.'</author>
   <description><![CDATA[<img src="'.$root.IMG_DIR.'s1/'.$id.'_'.$name.'" alt="thumnail" />]]></description>
   <media:group>
    <media:title>'.$name.'</media:title>
';
 if($len==12){ // No thumbnail
  $ITEMS .= '    <media:content medium="image" url="'.$root.IMG_DIR.$id.'_'.$name.'" width="'.$w.'" height="'.$h.'" />
    <media:thumbnail url="'.$root.IMG_DIR.$id.'_'.$name.'" width="'.$w.'" height="'.$h.'" />
';
 } elseif($len > 12){     // Thumbnail exists
  $file = $root.IMG_DIR.THUMB2.$id.'_'.$name;
  $ITEMS .= '    <media:content medium="image" url="'.$file.'" width="'.trim($split[11]).'" height="'.trim($split[12]).'" />
    <media:thumbnail url="'.$root.IMG_DIR.THUMB1.$id.'_'.$name.'" width="'.trim($split[8]).'" height="'.trim($split[9]).'" />
';
 }
 $ITEMS .= '   </media:group>
  </item>'.NL;
}

$mtime = date('D, d M Y H:i:s +0900', $id ? $id :$mod_date);

$MRSS=<<<__MRSS
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
 <channel>
  <title>$blog_name::mediaRSS</title>
  <link>$root</link>
  <description>mediaRSS for SlideShow</description>
  <lastBuildDate>$mod_date</lastBuildDate>
  <pubDate>$mtime</pubDate>
$ITEMS
 </channel>
</rss>

__MRSS;

rewrite('feeds/mrss.xml', $MRSS);

if(isset($images_cache)) return;

return $DIVISION['body'] .= '<p class="alert-p">MRSSファイルが作成されました</p>
 <p class="center" style="margin-top: 1em;" title="生成されたXMLファイル"><a href="'.$root.'/feeds/mrss.xml">[media RSS xml]</a></p>
';

?>